package com.tzbank.wisdom.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.entity.backstage.constants.BackStageConstants;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.dao.HomePageInfoMapper;
import com.tzbank.wisdom.entity.dto.HomePageDeployDto;
import com.tzbank.wisdom.service.CustomerCenterService;
import com.tzbank.wisdom.service.HomePageInfoService;
import com.tzbank.wisdom.utils.ThreadPoolFactory;

import net.sf.json.JSONObject;

/**
 * 首页信息查询service
 * @author wangbin
 *
 */
@Service(value = "homePageInfoService")
public class HomePageInfoServiceimpl implements HomePageInfoService{

	@Autowired
	private HomePageInfoMapper homePageInfoMapper;
	@Resource
	private CustomerCenterService customerCenterService;

	@Override
	public Map<String, Object> getListModuleInfo(Map<String, String> reqBody) throws Exception {
		/*String clientSeqNo = reqBody.get("clientSeqNo");
		Map<String, String> userMap = RedisUtils.getUserInfo(clientSeqNo);
		if (userMap == null) {
			return ResultUtil.resultMap("请重新登录", ResultCode.RESUTL_CODE_FAILURE.code(), null);
		}*/
		Map<String, String> userMap = new HashMap<String,String>();
		 userMap.put("clientNo","1300107966");
		 //查询客户经理信息ESB接口
		 Future<HashMap> future = this.getCustomerInfo(userMap);
		
		//异步组装首页模块信息
		Map<String, Object> map = new HashMap<String, Object>();
		List<HomePageDeployDto> module01List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_01);
		List<HomePageDeployDto> module02List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_02);
		List<HomePageDeployDto> module03List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_03);
		List<HomePageDeployDto> module04List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_04);
		List<HomePageDeployDto> module05List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_05);
		List<HomePageDeployDto> module06List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_06);
		List<HomePageDeployDto> module07List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_07);
		List<HomePageDeployDto> module08List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_08);
		List<HomePageDeployDto> module09List = homePageInfoMapper.getListModuleInfo(BackStageConstants.module_09);
		
		map.put("module_02", module02List);
		map.put("module_03", module03List);
		map.put("module_04", module04List);
		map.put("module_05", module05List);
		map.put("module_06", module06List);
		map.put("module_07", module07List);
		map.put("module_08", module08List);
		map.put("module_09", module09List);
		//Map<String,Object> cusmap = customerCenterService.getCustomerMgrInfo(userMap);
		HashMap hmap = future.get();
		JSONObject dataJson = JSONObject.fromObject(hmap.get("data"));
		//HomePageDeploy hpd = new HomePageDeploy();
		//hpd.setTitle("客户经理"); 
		/*String manageName = dataJson.get("custManagerName").toString();
		String manageId = dataJson.get("custManagerId").toString();*/
		if(hmap != null && dataJson.get("custManagerName") != null && dataJson.get("custManagerId") != null) {
			module01List.get(0).setTitleMessage(dataJson.get("custManagerName")+"-工号"+dataJson.get("custManagerId"));
		}else {
			module01List.get(0).setTitleMessage("");
		}
		map.put("module_01", module01List);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), map);
	}
	
	private Future<HashMap> getCustomerInfo(Map<String, String> userMap) throws Exception{
		//ThreadPool threadpool = ThreadPoolFactory.getThreadPool()(new ThreadPoolConfig(5,20,7 * 24 * 3600,20));
		return (Future<HashMap>) ThreadPoolFactory.getThreadPool().submit(new Callable<HashMap>() {
			@Override
			public HashMap call() throws Exception {
				return (HashMap) customerCenterService.getCustomerMgrInfo(userMap);
			}
		});
	}

}
